/*
 * @Author: LGX
 * @Date: 2022-04-11 19:06:44
 * @LastEditors: LGX
 * @LastEditTime: 2022-04-11 20:09:10
 * @FilePath: \erp-management-system\src\components\customerseas\Delete.jsx
 * @Description: 删除Modal框
 * 
 */
import React, { useState, useRef } from 'react'
import { Input, Space, Button, message } from 'antd';
import CheckBtn from '../../components/customerseas/CheckBtn';
import myAxios from '../../utils/myAxios';
import store from '../../redux/store';
import moment from 'moment'
import { AsyncTableDataAction } from '../../redux/action/customerAction/tableData';

export default function Delete(props) {
    const { TextArea } = Input;
    const text = useRef()
    const { setisShowDeleta, userInfo, deleStatus } = props;
    //复选按钮数组
    const checkArr = [
        { id: 1, title: '联系人', check: false },
        { id: 2, title: '跟单', check: false },
        { id: 3, title: '订单', check: false },
        { id: 4, title: '合同', check: false },
        { id: 5, title: '售后', check: false },
        { id: 6, title: '财务', check: false },
        { id: 7, title: '工单', check: false },
        { id: 8, title: '附件', check: false },
    ]
    const [stateBtn, setstateBtn] = useState(checkArr);

    //子组件按钮传递id
    const getChecked = (id) => {
        stateBtn.forEach((item) => {
            if (item.id == id) {
                item.check = !item.check
            }
        })
        setstateBtn(stateBtn.concat([]))
    }
    //取消
    const del = () => {

        setisShowDeleta(false)
    }

    //提交删除
    const submitDelete = () => {
        let relationList = []
        //获取哪些按钮被点击了
        const checkBtnArr = stateBtn.filter((item) => item.check)
        checkBtnArr.forEach(item => {
            relationList.push(item.id)
        })
        //获取删除原因
        let deleteReason = text.current.resizableTextArea.textArea.value;
        //整合所有需要的数据
        let deleteCustomerVo = {
            deleteReason,
            id: userInfo.id,
            relationList,
            deleteTime: moment().format('YYYY-MM-DD h:mm:ss')
        }
        console.log(deleteCustomerVo);
        //发起删除请求
        myAxios({
            method: 'post',
            url: '/gongHai/deletePublicCustomerById',
            data: deleteCustomerVo
        })
            .then(res => {
                // console.log(res.data);
                if (res.data.code == 200) {
                    setTimeout(() => {
                        setisShowDeleta(false)
                    }, 1000)
                    message.success('删除成功！')
                    deleStatus()
                } else {
                    message.error(res.data.msg)
                }
            })

    }

    return (
        <div>
            <h3 >删除</h3>
            <div style={{ fontWeight: 'bolder', textAlign: 'center' }}>删除 原因</div>
            <TextArea ref={text} style={{ margin: '10px 10px 10px 0px' }} rows={3} />
            <div>同时删除以下关联记录 <span style={{ color: 'red' }}>(无法恢复)</span> ：</div>


            <CheckBtn click={getChecked} stateBtn={stateBtn} />

            <div style={{ margin: '10px 10px 30px 0px' }}>已选中客户编号：<span>{userInfo.id}</span> </div>
            <div style={{ textAlign: 'center', borderTop: '1px dashed gray', padding: 20 }}>
                <Space>
                    <Button style={{ width: 100 }} size='midle' type="primary" htmlType="submit"
                        onClick={() => {
                            submitDelete()
                        }
                        }
                    >
                        保存
                    </Button>
                    <Button onClick={del} style={{ width: 100 }} size='midle' danger type="primary" >
                        取消
                    </Button>
                </Space>
            </div>
        </div>
    )
}
